package map.android.baidu.carpool.page.adpater;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import map.android.baidu.carpool.R;
import map.android.baidu.carpool.response.DriverCommentListResponse.DriverCommentItem;

import com.baidu.components.api.extra.APIProxy;

import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

/**
 * @Author chenlei18
 * @Description  车主评价适配器
 * @CreateTime 2015-3-28 下午12:59:33
 */
public final class EvaluteDriverAdapter extends BaseAdapter {
    
    private ArrayList<DriverCommentItem> mCommentList;

    public EvaluteDriverAdapter() {
        mCommentList = new ArrayList<DriverCommentItem>();
    }

    @Override
    public int getCount() {
        return mCommentList.size();
    }


    @Override
    public Object getItem(int position) {
        return null;
    }


    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHold viewHold;
        if (convertView == null) {
            viewHold = new ViewHold();
            convertView = APIProxy.res().inflate(R.layout.evaluate_driver_item);
            viewHold.line =  convertView.findViewById(R.id.evalute_item_line);
            viewHold.userName = (TextView) convertView.findViewById(R.id.user_name);
            viewHold.userEvaluteTime = (TextView) convertView.findViewById(R.id.user_evalute_time);
            viewHold.userEvaluteContent = (TextView) convertView.findViewById(R.id.user_evalute_content);
            convertView.setTag(viewHold);
        } else
            viewHold = (ViewHold) convertView.getTag();

        if(mCommentList.get(position)!=null){
            viewHold.userName.setText(mCommentList.get(position).userNickname);
            viewHold.userEvaluteTime.setText(getResultTime(mCommentList.get(position).createTime));
            viewHold.userEvaluteContent.setText(mCommentList.get(position).commentMsg);
        }
        return convertView;
    }
    
    /**
     * 将时间戳转化为 目标格式时间
     * @param strTime 时间戳字符串
     * @return
     */
    private String getResultTime(String strTime) {
        if(strTime == null)
            return null;
        String resultTime = null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd  HH:mm");
        long time = Long.valueOf(strTime);
        resultTime = sdf.format(new Date(time * 1000L));
        return resultTime;
    }

    final class ViewHold {
        View line;
        TextView userName;
        TextView userEvaluteTime;
        TextView userEvaluteContent;
    }
    
    public void setData(ArrayList<DriverCommentItem> commentList) {
        this.mCommentList = commentList;
    }
    
    public void addData(ArrayList<DriverCommentItem> commentList) {
        this.mCommentList.addAll(commentList);
    }

}
